<?php
require("cap.php");
require_once("classes/App.php");
$app = new App();
	$app->Cap($var_cap_rates, $var_title);
	$app->printMenu($var_main, $var_admin, $var_rates, $var_exit, TRUE);
	if(isset($_POST['add_new'])){
		$new_line = mysql_escape_string($_POST['new_line']);
		$new_pmask = mysql_escape_string($_POST['new_pmask']);
		$new_price = mysql_escape_string($_POST['new_price']);
		if((strlen($new_line) != 0) and (strlen($new_pmask) != 0) and (strlen($new_price) != 0)){
			if($_POST['internal'] === 'on')
				$internal = 1;
			else
				$internal = 0;
			$app->myQuery("INSERT INTO `price` ( `line`, `pmask`, `price` ) VALUES ( '".$new_line."', '".$new_pmask."', '".$new_price."' )",
							"trixbi");
		}
	}
	if(isset($_GET['drop'])){
		$app->myQuery("DELETE FROM `price` WHERE `pid`='".$_GET['drop']."'",
						"trixbi");
	}
	
	echo '<table>
			<tr><td id="td_border">'.$var_direction.'</td><td id="td_border">'.$var_mask.'</td><td id="td_border">'.$var_price.'</td></tr>
			<form method="POST">';
	echo '<tr><td id="td_border"><input type="text" name="new_line" /></td><td id="td_border"><input type="text" name="new_pmask" onmouseover="Tip(\''.$var_mask_tip.'\')" onmouseout="UnTip()" />
			</td><td id="td_border"><input type="text" name="new_price" /></td></tr><tr><td></td><td></td><td align="right"><input type="submit" name="add_new" value="'.$var_add.'"></td></tr>';
	$_price = $app->myQuery("SELECT * FROM `price`", "trixbi", TRUE);
	if(isset($_price))
	foreach($_price as $_price){
		$shChButton = TRUE;
		$pid = $_price['pid'];
		$line = $_price['line'];
		$pmask = $_price['pmask'];
		$price = $_price['price'];
		if(isset($_POST['submit'])){
			$_line = mysql_escape_string($_POST['line'.$pid]);
			$_pmask = mysql_escape_string($_POST['pmask'.$pid]);
			$_price = mysql_escape_string($_POST['price'.$pid]);
			($_POST['internal'.$pid] === 'on') ? $_internal = 1 : $_internal = 0;
			if((($_line !== $line) and (strlen($_line) != 0)) or
				(($_pmask !== $pmask) and (strlen($_pmask) != 0)) or
				(($_price !== $price)) and (strlen($_price) != 0)){
					$app->myQuery("UPDATE `price` SET `line`='".$_line."', `pmask`='".$_pmask."', `price`='".$_price."' WHERE `pid`='".$pid."'",
									"trixbi");
					$line = $_line;
					$pmask = $_pmask;
					$price = $_price;
			}
		}	
		echo '<tr><td id="td_border"><input type="text" name="line'.$pid.'" value="'.$line.'" /></td><td id="td_border">
				<input type="text" name="pmask'.$pid.'" value="'.$pmask.'" /></td><td id="td_border">
				<input type="text" name="price'.$pid.'" value="'.$price.'" /></td><td><a href="price.php?drop='.$pid.'" border="0">
				<img src="./img/delete.png"></a></td></tr>';
	}
	if($shChButton)
		echo '<tr><td></td><td></td><td align="right"><input type="submit" name="submit" value="'.$var_change.'" /></td></tr></form></table>';
	echo '</body></html>';	
unset($app);
?>
